﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Eagles.Application.Job.DataAccess;
using Eagles.Base;
using Eagles.Base.Configuration;
using Eagles.Base.DataBase;
using Eagles.Base.DataBase.Implement;
using Eagles.Base.Logger;
using Eagles.Base.Logger.Implement;
using Eagles.DomainService.Model.Config;
using Eagles.DomainService.Model.User;

namespace Eagles.Application.Job.Logic
{


    public class UserInfoLogic
    {

        private static readonly ILogger Logger = new Logger();
        private static readonly IConfigurationManager Configuration = new Eagles.Base.Configuration.Implement.ConfigurationManager();
        private static readonly IDbManager DbManager = new DbManager(Configuration, Logger);


        public void StartUserInfo()
        {

            Logger.LoggerInfo($"{DateTime.Now.ToShortTimeString()} 开始处理用户数据");

            // var joinNotice = Configuration.GetConfiguration<EaglesConfiguration>().JoinNotice;

            // var payNotice = Configuration.GetConfiguration<EaglesConfiguration>().PayNotice;

            var sysJoinNotice = Configuration.GetConfiguration<EaglesConfiguration>().SysJoinNotice;

            var sysPayNotice = Configuration.GetConfiguration<EaglesConfiguration>().SysPayNotice;

            var sysNoticeUrl = Configuration.GetConfiguration<EaglesConfiguration>().SysNoticeUrl;

            var us = new UserInfoDataAccess(DbManager);


            List<TbUserInfo> userinfo = us.GetUserInfoList();

            foreach (var mod in userinfo)
            {
                try
                {

                    int payNotice = mod.FeeExpireNoticeDay;

                    if (mod.MemberTime != null)
                    {
                        var tdate = DateTime.Now;
                        var bdate = mod.MemberTime.Value;

                        if (tdate.Month.CompareTo(bdate.Month) == 0 && tdate.Day.CompareTo(bdate.Day) == 0 && tdate.Year.CompareTo(bdate.Year) > 0)
                        {

                            var notice = new TbUserNotice()
                            {
                                Content = sysJoinNotice
                                    .Replace("${toUser}", mod?.Name)
                                    .Replace("${year}", (tdate.Year - bdate.Year).ToString()),
                                UserId = mod.UserId,
                                FromUser = 0,
                                NewsType = 40,
                                OrgId = mod.OrgId,
                                Title = "入党纪念日提醒",
                                IsRead = 1,
                                TargetUrl = string.Format(sysNoticeUrl, mod.OrgId)

                            };


                            us.SendUserNotice(notice);

                        }
                    }

                    if (mod.FeeExpireDate != null && mod.FeeNotice > 0)
                    {

                        //
                        DateTime tDate = DateTime.Now;
                        DateTime expireDate = mod.FeeExpireDate.Value;
                        TimeSpan ts = expireDate - tDate;
                        

                        //var tdate = DateTime.Now;
                        //var bdate = mod.FeeExpireDate.Value.AddDays(-payNotice);

                        if (ts.Days <= payNotice && tDate.CompareTo(expireDate) < 0)
                        {


                            var notice = new TbUserNotice()
                            {
                                Content = sysPayNotice
                                    .Replace("${toUser}", mod?.Name)
                                    .Replace("${day}",
                                        ts.Days.ToString()),
                                UserId = mod.UserId,
                                FromUser = 0,
                                NewsType = 41,
                                OrgId = mod.OrgId,
                                Title = "党费到期提醒通知",
                                IsRead = 1,
                                TargetUrl = string.Format(sysNoticeUrl, mod.OrgId)

                            };


                            us.SendUserNotice(notice);

                        }
                    }



                }
                catch (TransactionException err)
                {
                    Logger.LoggerError(err.ToString());
                }
                catch (Exception err)
                {
                    Logger.LoggerError(err.ToString());

                }

            }

            Logger.LoggerInfo($"{DateTime.Now.ToShortTimeString()} 处理用户数据完成");

        }

    }
}
